package com.chat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.chat.domain.ChatAccount;
import com.chat.request.AccountDetailRequest;
import com.chat.request.AccountIdRequest;
import com.chat.response.BaseResponse;
import com.chat.response.ChatAccountDetailResponse;
import com.chat.response.ChatAccountResponse;
import com.chat.service.AccountDetailService;
import com.chat.service.ChatAccountService;
import com.chat.service.ChatGroupService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("/cat/user")
public class ChatAccountController {
    @Resource
    AccountDetailService accountDetailService;
    @Resource
    ChatAccountService catAccountService;
    @Resource
    ChatGroupService   catGroupService;

    @PostMapping("/detail")
    public BaseResponse<ChatAccountDetailResponse> getUserDetail(@Validated @RequestBody AccountIdRequest Request) {
        ChatAccountDetailResponse userDetail = accountDetailService.getUserDetail(Request);
        return BaseResponse.success(userDetail);
    }

    @PostMapping("/update")
    public BaseResponse<String> updateUserDetail(@RequestBody AccountDetailRequest detail) {

        Integer i = accountDetailService.updateUserDetail(detail);
        if (i == 0) {
            return BaseResponse.failure(200, "修改失败！");
        } else
            return BaseResponse.success("修改成功！");
    }

    @GetMapping("/{id}")
    public BaseResponse<Object> getUser(@PathVariable String id) {
        ChatAccount byId = catAccountService.getById(id);
        Object bean = BeanUtil.toBean(byId, ChatAccountResponse.class);
        if (bean == null) {
            bean = catGroupService.getById(id);
        }
        return BaseResponse.success(bean);
    }
}
